<?php
class RecipesController extends Zend_Controller_Action
{
    protected $_manager;
    protected $_title = "Вегетарианские рецепты";
    
    public function init()
    {
        $this->_manager = new Default_Model_Managers_Recipes();
        
    	$user = Zend_Auth::getInstance()->getStorage()->read();
        $this->_user = $user;
        if ($user instanceof Default_Model_Entities_Profile) {
        	$roleType = $user->getRoleType();
        }
        
        if (isset($roleType) && $roleType == 'Administrator') {
        	$this->view->roleType = $roleType;
        } else {
        	$this->view->roleType = "User";
        }
        
        $this->view->title = $this->_title;
    }
    
    public function indexAction()
    {
    	$foodType = $this->_request->getParam("foodType");
    	$page = $this->_request->getParam("page");

        $this->view->recipes = $this->_manager->getRecipesList($foodType, $page);
        $this->view->pagesCount = $this->_manager->getPagesCount($foodType);
        $foodTypeName = $this->_manager->getFoodTypeName($foodType);
        $foodTypeName = mb_strtoupper(mb_substr($foodTypeName, 0, 1)) . mb_substr($foodTypeName, 1);
        $this->view->foodTypeName = $foodTypeName;
        $this->view->foodType = $foodType;
        $this->view->title = $foodTypeName . ". " . $this->_title;
    }
    
	public function recipeAction()
    {
    	$page = $this->_request->getParam("page");
        $recipe = $this->_manager->getRecipeById($page);
        $this->view->recipe = $recipe;
        $this->view->title = $recipe['recipe_name'] . ". " . $this->_title;
    }
    
    public function foodTypesAction()
    {
    	$foodTypes = $this->_manager->getFoodTypes(1);
    	$this->view->foodTypes = $foodTypes; 
    	
    	$front = Zend_Controller_Front::getInstance();
		$foodType = $front->getRequest()->getParam("foodType");
    	
    	$this->view->foodType = $foodType;
    }
    
    public function mainIngredientsAction()
    {
    	$this->view->ingredients = $this->_manager->getMainIngredients();
    }
    
    public function ingredientAction()
    {
    	$front = Zend_Controller_Front::getInstance();
    	$ingredient = $front->getRequest()->getParam("ingredient");
    	$ingredient = mb_strtoupper(mb_substr($ingredient, 0, 1)) . mb_substr($ingredient, 1);
    	$page = $front->getRequest()->getParam("page");
    	$this->view->ingredientName = $ingredient;
    	$this->view->recipes = $this->_manager->getRecipesByIngredient($ingredient, $page);
    	$this->view->pagesCount = $this->_manager->getPagesCountByIngredient($ingredient);
    	    	
    	$this->view->title = $ingredient . ". " . $this->_title;
    }
}